
package com.ccp.dev.system.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.system.dao.SubSystemDao;
import com.ccp.dev.system.model.SubSystem;
import com.ccp.dev.system.model.SysRole;
import com.ccp.dev.system.model.SysUser;
import com.ccp.dev.system.util.CookieUitl;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 子系统管理 Service类
 * @author y
 */
@Service
public class SubSystemService extends BaseService<SubSystem> {
    @Resource
    SubSystemDao subSystemDao;
    @Resource
    SysRoleService sysRoleService;
    /**
     * 查询被激活的系统
     * @return list
     */
    public List<SubSystem> queryActiveSystem(){
        return subSystemDao.queryActiveSystem();
    }
    /**
     * 查询被激活的系统前端插件使用
     * @return list
     */
    public List<Map> queryActiveSystem_simple(){
        return this.subSystemDao.queryActiveSystem_simple();
    }

    /**
     * 取得拥有的系统
     * @param user
     * @return
     */
    public List<SubSystem> getByUser(SysUser user){
        List<SubSystem> sysList=new ArrayList<SubSystem>();
        if(user.getAuthorities().contains(SysRole.ROLE_GRANT_SUPER)){
            return queryActiveSystem();
        }else if (sysRoleService.isContainThreeRole(user.getUserId())){
            List<SysRole> allRole = sysRoleService.queryByUserId(user.getUserId());
            List<SysRole> xtgly = allRole.stream().filter(a -> a.getRoleType().equals(SysRole.DEFAULT_XTGLY)).collect(Collectors.toList());
            List<SysRole> aqbmgly = allRole.stream().filter(a -> a.getRoleType().equals(SysRole.DEFAULT_AQBMGLY)).collect(Collectors.toList());
            List<SysRole> aqsjy = allRole.stream().filter(a -> a.getRoleType().equals(SysRole.DEFAULT_AQSJY)).collect(Collectors.toList());
            if(!xtgly.isEmpty()){
                sysList.add(subSystemDao.getById(xtgly.get(0).getSystemId()));
            }
            if(!aqbmgly.isEmpty()){
                sysList.add(subSystemDao.getById(aqbmgly.get(0).getSystemId()));
            }
            if(!aqsjy.isEmpty()){
                sysList.add(subSystemDao.getById(aqsjy.get(0).getSystemId()));
            }
            return sysList;
        }else{
            String roleNames="";
            Collection<GrantedAuthority> roles= (Collection<GrantedAuthority>) user.getAuthorities();

            if(BeanUtils.isEmpty(roles)){
                return sysList;
            }
            for(GrantedAuthority auth:roles){
                if("".equals(roleNames)){
                    roleNames+="'" + auth.getAuthority() +"'";
                }
                else{
                    roleNames+=",'" + auth.getAuthority() +"'";
                }
            }
            Map<String,String> map=new HashMap<>();
            map.put("roleNames", roleNames);
            return subSystemDao.queryByRoles(map);
        }
    }

    /**
     * 写出子系统cookie
     * @param systemId 系统id
     * @param request   HttpServletRequest
     * @param response  HttpServletResponse
     */
    public void setCurrentSystem(String systemId,HttpServletRequest request, HttpServletResponse response){
        SubSystem subSystem=subSystemDao.getById(systemId);
        if(subSystem!=null){
            writeCurrentSystemCookie(String.valueOf(systemId),  request,  response);
            request.getSession().setAttribute(SubSystem.CURRENT_SYSTEM,subSystem);
        }
    }

    public void writeCurrentSystemCookie(String systemId, HttpServletRequest request, HttpServletResponse response){
        if (CookieUitl.isExistByName(SubSystem.CURRENT_SYSTEM, request)) {
            CookieUitl.delCookie(SubSystem.CURRENT_SYSTEM, request, response);
        }
        // 14 days
        int tokenValiditySeconds = 86400 * 14;
        CookieUitl.addCookie(SubSystem.CURRENT_SYSTEM, systemId, tokenValiditySeconds, request, response);
    }
}

